package com.cuckoo.cuckoo_app2.ui;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.apiAsync.BaseCallbac;
import com.cuckoo.cuckoo_app2.apiAsync.Imp.UserApiImp;
import com.cuckoo.cuckoo_app2.apiAsync.UserApi;
import com.cuckoo.cuckoo_app2.utils.DialogUtils;
import com.google.gson.JsonObject;

public class ChangePhoneAcitvity extends AppCompatActivity {

    private Context context;
    private ViewHolder holder;
    private UserApi userApi;
    private Integer color_grey;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        Log.i("TEST", "ChangePhoneAcitvity.onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone);

        this.context = this;
        this.holder = new ViewHolder();
        this.userApi = new UserApiImp();

        this.color_grey = ContextCompat.getColor(context, R.color.text_grey);

        this.initView();
    }

    @Override
    protected void onStart() {
        Log.i("TEST", "ChangePhoneAcitvity.onStart()");
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        Log.i("TEST", "ChangePhoneAcitvity.onDestroy()");
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        Log.i("TEST", "ChangePhoneAcitvity.onStop()");
        super.onStop();
    }

    public void initView() {
        // 获取手机验证码
        holder.cp_button_get.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onGetPhoneCode();
            }
        });

        // 点击确认按钮
        holder.cp_submint.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onSubmit();
            }
        });
    }

    /**
     * 发送手机验证码
     */
    public void onGetPhoneCode() {
        String mobile = holder.cp_phone.getText().toString();
        if (mobile.equals("")) {
            DialogUtils.showToast(context, "请输入手机号");
            return;
        }

        userApi.performGetChangePwdCodeAsync(mobile, new BaseCallbac<JsonObject>() {
            @Override
            public void onSuccess(JsonObject data) {
                if (!data.get("success").getAsBoolean()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            DialogUtils.showToast(context, data.get("message").getAsString());
                        }
                    });
                    return;
                }
                holder.cp_button_get.setText("已发送");
                holder.cp_button_get.setBackgroundColor(color_grey);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        DialogUtils.showToast(context, "验证码已发送");
                    }
                });
            }

            @Override
            public void onFailure(String errorMessage) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        DialogUtils.showToast(context, "验证码请求失败");
                    }
                });
            }
        });
    }

    /**
     * 提交
     */
    public void onSubmit() {
        String mobile = holder.cp_phone.getText().toString();
        String code = holder.cp_code.getText().toString();
        if (mobile.equals("")) {
            DialogUtils.showToast(context, "请输入手机号");
            return;
        }
        if (code.equals("")) {
            DialogUtils.showToast(context, "请输入验证码");
            return;
        }
        userApi.performChangePhoneAsync(mobile, code, new BaseCallbac<JsonObject>() {

            @Override
            public void onSuccess(JsonObject data) {
                if (!data.get("success").getAsBoolean()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            DialogUtils.showToast(context, data.get("message").getAsString());
                        }
                    });
                    return;
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        DialogUtils.showToast(context, data.get("message").getAsString());
                    }
                });

                // 暂停一秒
                DialogUtils.sleep(1000);

                // 返回上一页
                finish();
            }

            @Override
            public void onFailure(String errorMessage) {

            }
        });
    }

    /**
     * 获取相应的ID
     */
    class ViewHolder {

        private EditText cp_phone;
        private EditText cp_code;
        private Button cp_button_get;
        private Button cp_submint;

        public ViewHolder() {
            cp_phone = findViewById(R.id.cp_phone);
            cp_code = findViewById(R.id.cp_code);
            cp_button_get = findViewById(R.id.cp_button_get);
            cp_submint = findViewById(R.id.cp_submint);
        }
    }
}
